//项目入口
import Vue from "vue";

//1.1导入路由模块
import VueRouter from "vue-router";
Vue.use(VueRouter);

//注册vuex
import Vuex from "vuex";
Vue.use(Vuex);

//每次进入app 会调用main.js,然后把存储的数据读出来把 字符串 转成 数组
var car = JSON.parse(localStorage.getItem("car") || "[]");

//store存储对象
var store = new Vuex.Store({
  state: {
    shopCar: car //购物车是个[]数组,假设里面有对象{id:商品id,pirce:商品价格,count:商品数量,selected:true}
  },
  mutations: {
    addToCar(state, shopcarinfo) {
      //增加购物车数据
      //默认为false
      var flag = false;
      //如果商品在购物车里只改变数量
      state.shopCar.some(item => {
        if (item.id == shopcarinfo.id) {
          item.count += parseInt(shopcarinfo.count);
          flag = true;
          return true;
        }
      });
      //如果商品不购物车在则push过去
      if (!flag) {
        state.shopCar.push(shopcarinfo);
      }
      //把最新数据push到本地
      localStorage.setItem("car", JSON.stringify(state.shopCar));
    },
    updateinfo(state, shopcarinfo) {
      //更新购物车数据
      state.shopCar.some(item => {
        if (item.id == shopcarinfo.id) {
          item.count = parseInt(shopcarinfo.count);
          return true;
        }
      });
      //把最新数据push到本地
      localStorage.setItem("car", JSON.stringify(state.shopCar));
    },
    removeinfo(state, id) {
      //从购物车删除
      state.shopCar.some((item, i) => {
        if (item.id == id) {
          state.shopCar.splice(i, 1);
          return true;
        }
      });
      //把最新数据push到本地
      localStorage.setItem("car", JSON.stringify(state.shopCar));
    },
    updateselected(state,info){
      state.shopCar.some(item=>{
        if(item.id == info.id){
          item.selected = info.selected
      }
      })
      //把最新数据push到本地
      localStorage.setItem("car", JSON.stringify(state.shopCar));
    }
  },
  getters: {
    getAllcount(state) {
      var c = 0;
      state.shopCar.forEach(item => {
        c += item.count;
      });
      return c;
    },
    getAllselected(state){ //获取所有选中情况
      var o = {}
      state.shopCar.forEach(item=>{
        o[item.id] = item.selected
      })
      return o 
    },
    getNumadd(state){
      var o = {
        count:0,
        numadd:0
      }
      state.shopCar.forEach(item=>{
        if(item.selected){
          o.count += item.count //选中总数
          o.numadd += item.pirce * item.count //总价
        }
      })
      return o
      
    }

  }
});

//导入安装 cnpm i moment -S 用来获取时间
import moment from "moment";
//定义时间过滤器
Vue.filter("dataFomart", function(dataStr, pattern = "YYYY-MM-DD HH:mm:ss") {
  return moment(dataStr).format(pattern);
});

//2.1获取数据 需要安装 cnpm i vue-resource -S
//2.2导入vue-resource
import VueResource from "vue-resource";
Vue.use(VueResource);
//设置请求根路径
Vue.http.options.root = "http://47.89.21.179:8080/"; //黑马api
//设置请求post 表单格式
Vue.http.options.emulateJSON = true;

//导入MUI组件
import "./lib/css/mui.min.css";
import "./lib/css/icons-extra.css";

//1.2引入router.js
import router from "./router.js";

//导入注册 Mint-UI 组件
// import { Header, Swipe, SwipeItem ,Button ,Lazyload} from "mint-ui";
// Vue.component(Header.name, Header);
// Vue.component(Swipe.name, Swipe);
// Vue.component(SwipeItem.name, SwipeItem);
// Vue.component(Button.name, Button);
// Vue.use(Lazyload);
//导入全部 mint-ui组件
import MintUI from "mint-ui";
import "mint-ui/lib/style.css";
Vue.use(MintUI);

//导入 图片预览vue-preview
import VuePreview from "vue-preview";
Vue.use(VuePreview);

//导入App组件
import app from "./App.vue";

var vm = new Vue({
  el: "#app",
  render: c => c(app),
  router, //1.3 挂在路由对象到vm实例
  store
});
